<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
$company = 'unknown';


include_once('config/config.php');
/*
include_once('library/autoload.php');
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookCanvasLoginHelper;
FacebookSession::setDefaultApplication(fb_id,fb_secret);
session_start();
$helper = new FacebookRedirectLoginHelper('http://whatami.logicanica.com/');
try {
    $session = $helper->getSessionFromRedirect();
    
    if ($session) {
        
    }else{
        $loginUrl = $helper->getLoginUrl();
        header("Location: ".$loginUrl);
        exit();
    }

} catch (FacebookRequestException $e) {
} catch (\Exception $e) {}
*/
if (isset($_POST['cell'])) {
    
    try {
        $pdo = new PDO(dsn, user, password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'));
    
        $cell = (isset($_POST['cell']) && !empty($_POST['cell'])) ? trim($_POST['cell']) : "";
        $sql = sprintf("SELECT O.nombre,O.id FROM system_prefijo P INNER JOIN system_operadora O ON(P.operadoraid=O.id) WHERE digito='%s'",substr($cell, 0, 4));
        $stmt = $pdo->prepare($sql);
        $status = $stmt->execute();
        $company="";
        
        if ($status && $stmt->rowCount()>0) {
            $exist = $stmt->fetch();
            $company = ($exist) ? $exist['nombre'] : 'desconocido';
            $sql=sprintf("INSERT INTO system_userinfo(phone,operadoraid) VALUES('%s',%d)",$cell,$exist['id']);
            $stm = $pdo->prepare($sql);
            $st = $stm->execute();
            
        }
    
        header('content-type: application/json; charset=utf-8');
        echo json_encode(array('operator' => $company));
        $stmt=null;
        exit();
    }catch(Exception $ex){
        header('content-type: application/json; charset=utf-8');
        echo json_encode(array('error' => array("code"=>$ex->getCode(),"message"=>$ex->getMessage())));
        $stmt=null;
        exit();
    }
    catch(PDOException $ex){
        header('content-type: application/json; charset=utf-8');
        echo json_encode(array('error' => array("code"=>$ex->getCode(),"message"=>$ex->getMessage())));
        $stmt=null;
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="es" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:og="http://ogp.me/ns#"
      xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <title>Whatami - ¿Es movistar o claro?</title>
        <meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta property="og:title" content="Whatami - ¿Es movistar o claro?"/>
        <meta property="og:type" content="website"/>
        <meta property="og:url" content="http://whatami.logicanica.com/"/>
        <meta property="og:image" content="http://whatami.logicanica.com/images/screen.png?rand=1"/>
        <meta property="og:locale" content="es_ES" />
        <meta property="og:locale:alternate" content="en_GB" />
        <meta property="og:site_name" content="Whatami"/>
        <meta property="og:description" content="...Si lo que querés saber, es la compañia telefónica de ese número que vas a llamar No estés adivinando! Solo ingresa el número¡aqui!"/>

        <link href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link href='http://fonts.googleapis.com/css?family=Annie+Use+Your+Telescope' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="/css/foundation.min.css?v=5.2.0" type="text/css" />
        <link rel="stylesheet" href="/css/whatami.css?<?=time();?>" type="text/css" media="screen" />
        <script src="/js/vendor/modernizr.min.js?fdtn=5.2.0" type="text/javascript"></script>
        <!-- <link rel="stylesheet" href="/css/whatami.mb.css" type="text/css" media="handheld" />-->
        <!-- Diseño: Vanessa Lugo -->
        <!-- Programación: Irving Medina -->
    </head>
    <body>
    	<div id="fb-root"></div>
        <div id="socialbar" class="row">
        	<div class="small-12 medium-8 large-8 columns">
                    <h1 class="small-only-text-center text-left">Whatami</h1>
                    <h3 class="subheader small-only-text-center">¿Es movistar o claro?</h3>
        	</div>
	        <div class="hide-for-small-only medium-4 columns text-right">
                    <div class="fb-like" data-href="http://whatami.logicanica.com" data-layout="box_count" data-action="like" data-show-faces="true" data-share="false"></div>
	        </div>
        </div>
        <div id="panel-main" class="row">
        	<div class="medium-6 medium-push-6 columns">
        		<form name="whatami" id="whatami" method="post" data-abide="ajax">
                            <p class="calvin text-center"><span class="hide-for-small-only">...Si lo que querés saber, es la <br />compañia telefónica de ese <br />
                            número que vas a llamar<br />
                            No estés adivinando!<br /><br /></span>
                            Solo ingresa el número
                            <span class="operator here">¡aqui!</span>
                        </p>

                        <div class="row phone-field">
                            <div class="large-8 medium-10 small-centered columns">
                                    <input lang="es" type="text" pattern="[0-9]{8}" maxlength="8" placeholder="Número de teléfono..." class="text-center" name="cell" id="cell" value="" title="Escriba un número de teléfono válido(87654321)" autofocus = "autofocus" required />
                                    <small id="news-alert" class="error">Un número de teléfono es requerido</small>
                                    <small id="news-success" class="success hide">El número de teléfono es obligatorio</small>
                            </div> 	
                        </div>
                        <div class="row">
                            <p class="message large-8 medium-10 small-centered columns text-center"></p>    
                        </div>
                        <div class="row">
                            <div class="large-6 medium-8 small-10 small-centered columns">
                                <ul class="button-group even-2">
                                    <li><button type="submit" id="submitbtn" class="small button [radius round]">Verificar</button></li>
                                    <li><button type="reset" id="resetbtn" class="small button disabled">Limpiar</button></li>
                                </ul>
                            </div>
                        </div>
                </form>
        	</div>
                <div id="panel-img" class="hide-for-large-up medium-6 medium-pull-6 text-center columns">
                    <img data-interchange="[/images/bubble.png, (small)], [/images/woman.png, (medium)], [/images/globomujer.png, (large)]" data-uuid="interchange-i2815iob0" src="/images/globomujer.png">
        	</div>
                <div id="panel-img-absolute" class="show-for-large-up medium-6 medium-pull-6 columns">
                    <img data-interchange="[/images/bubble.png, (small)], [/images/woman.png, (medium)], [/images/globomujer.png, (large)]" data-uuid="interchange-i2815iob0" src="/images/globomujer.png">
        	</div>
        </div>
		<script src="/js/prefixfree.min.js" type="text/javascript"></script>
		<script src="/js/vendor/jquery.min.js?fdtn=5.2.0" type="text/javascript"></script>
		<script src="/js/foundation.min.js?v=5.2.0" type="text/javascript"></script>
		<script src="/js/whatami.js?<?=time()?>" type="text/javascript"></script>
		<script>(function(d, s, id) {
		  var js, fjs = d.getElementsByTagName(s)[0];
		  if (d.getElementById(id)) return;
		  js = d.createElement(s); js.id = id;
		  js.src = "//connect.facebook.net/es_LA/all.js#xfbml=1&appId=163994957013512";
		  fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));
		</script>
                <script>
                    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

                    ga('create', 'UA-11565430-1', 'auto');
                    ga('require', 'displayfeatures');
                    ga('send', 'pageview');

                </script>
    </body>
</html>